package com.itheima.cglib;

import com.itheima.aspect.MyAspect;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Method;

import org.junit.jupiter.api.Test;
import org.springframework.cglib.proxy.*;

public class CglibProxy implements MethodInterceptor {
    public Object createProxy(Object target){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        //创建切面类对象
        MyAspect myAspect = new MyAspect();
        //前增强
        myAspect.check_Permissions();
        //目标方法执行
        Object o1 = methodProxy.invokeSuper(o,objects);
        //后增强
        myAspect.log();
        return o1;
    }
}
